/**
*  
* @Function 批次数据服务 
* @ProjectName credit-data-platform 
* @PackageName com.py.credit.service.impl    
* @FileName BatchDataServiceImpl.java    
* @Date 2018年9月27日上午11:47:38
* @Author Min DongXv
* @Copyright (c) 2018, 金电联行 All Rights Reserved.   
*   
*/
package com.py.credit.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSONObject;
import com.py.credit.dao.BatchDataDao;
import com.py.credit.dao.DataEntryDao;
import com.py.credit.dao.TableDao;
import com.py.credit.entity.BatchFile;
import com.py.credit.entity.Column;
import com.py.credit.entity.ErrorData;
import com.py.credit.entity.Table;
import com.py.credit.service.BatchDataService;
import com.py.credit.service.ColumnService;
import com.py.credit.service.TableService;
import com.py.credit.utils.DateUtil;
import com.py.credit.utils.SQLBuilder;
import com.py.framework.core.common.Page;

/**
 * 
 * @Function 批次数据服务
 * @ClassName BatchDataServiceImpl
 * @Date 2018年9月27日 上午11:47:38
 * @Author Min DongXv
 * 
 */
@Service
public class BatchDataServiceImpl implements BatchDataService {

	@Autowired
	private TableService tableService;

	@Autowired
	private ColumnService columnService;

	@Autowired
	private BatchDataDao batchDataDao;

	@Autowired
	private DataEntryDao dataEntryDao;
	
	@Autowired
	private TableDao tableDao;

	/**
	 *
	 * @Function 获取批次导入的数据
	 * @FileName BatchDataServiceImpl.java
	 * @MethodName batchDataByConditionForPage
	 * @param columns
	 * @param additionalColumns
	 * @param condition
	 * @param page
	 * @param batchId
	 *            批次ID
	 * @return
	 * @Date 2018年9月27日 上午11:48:02
	 * @Author Min DongXv
	 *
	 */
	@Override
	public List<Map<String, Object>> batchDataByConditionForPage(List<String> columns, String[] additionalColumns,
			String condition, Page page, Integer tableId, String batchId, Long deptId, Long userId) {
		// 获取表信息
		Table table = tableService.queryCubeById(tableId);
		if (null != table && StringUtils.isNotBlank(table.getResourceId())) {
			// 获取所有列
			List<Column> tableColumns = columnService.getListByTableId(table.getTableId());
			// 拼接表名
			String tableName = "t_" + table.getResourceId();
			List<String> conditions = SQLBuilder.createConditionSql(condition);
			// 查询批次数据
			List<Map<String, Object>> data = batchDataDao.batchDataByConditionForPage(columns, additionalColumns,
					conditions, page, tableName, batchId,deptId,userId);
			if (!CollectionUtils.isEmpty(data)) {
				for (Map<String, Object> m : data) {
					if (!CollectionUtils.isEmpty(m)) {
						m.put("errorMess", null);
						m.put("typeMsgReason", null);
						m.put("typeMsg", "录入成功");
						if ("E".equals(m.get("status"))) {
							// 获取异常信息集合
							List<ErrorData> errorDatas = dataEntryDao.getErrorDataListForTableId(tableId,
									Integer.parseInt(String.valueOf(m.get("record_id"))));
							if (!CollectionUtils.isEmpty(errorDatas)) {
								Map<String, String> err = new HashMap<>(errorDatas.size());
								Set<String> msg = new HashSet<>(3);
								String typeMsgReason = "";
								// 表规则字段规则提示处理
								for (ErrorData e : errorDatas) {
									if (null != e) {
										if ("Z".equals(e.getType())) {
											msg.add("字段规则校验失败");
											err.put(e.getColunmEN(), e.getReason());
										} else if ("B".equals(e.getType())) {
											msg.add("表规则校验失败");
											typeMsgReason = e.getReason();
										} else if ("S".equals(e.getType())) {
											msg.add("数据库规则校验失败");
											typeMsgReason = e.getReason();
											String jsonValue = e.getColunmValue();
											// 进行数据填充
											if (StringUtils.isNotBlank(jsonValue)
													&& !CollectionUtils.isEmpty(tableColumns)) {
												JSONObject jb = JSONObject.parseObject(jsonValue);
												for (Column c : tableColumns) {
													String code = c.getMetricCode();
													m.put(code, jb.getString(code));
												}
											}
										}
									}
								}
								m.put("typeMsgReason", typeMsgReason);
								m.put("errorMess", err);
								m.put("typeMsg", StringUtils.join(msg.toArray(), ","));
							}
						}
					}
				}
				return data;
			}
		}
		return new ArrayList<>();
	}

	@Override
	public Map<String, Object> batchDataDeptRun() {
		int a = batchDataDao.updateFrxkDept();
		int b = batchDataDao.updateFrcfDept();
		int c = batchDataDao.updateZrrxkDept();
		int d = batchDataDao.updateZrrcfDept();
		
		Map<String, Object> map = new HashMap<>();
		map.put("资源表部门更新时间", DateUtil.INSTANCE.getCurrDate());
		map.put("法人许可", a);
		map.put("法人处罚", b);
		map.put("自然人许可", c);
		map.put("自然人处罚", d);
		return map;
	}

	@Override
	public List<Map<String, Object>> getlList(Page page, List<String> deptIds, String startTime, String endTime) {
		List<Map<String, Object>> list = new ArrayList<>();
		 
		list = tableDao.getXkCblListForPage(page, deptIds, endTime, endTime);
		
		list = tableDao.getCfCblListForPage(page, deptIds, endTime, endTime);
		
		return list;
	}
}
